package com.zuofei.xmpp.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zuofei.xmpp.R;
import com.zuofei.xmpp.fragment.ContactsFragment;
import com.zuofei.xmpp.fragment.SessionFragment;
import com.zuofei.xmpp.utils.ToolBarUtil;

import java.util.ArrayList;
import java.util.List;

public class MsgPanelActivity extends AppCompatActivity implements SessionFragment.OnFragmentInteractionListener, ContactsFragment.OnFragmentInteractionListener {
    ViewPager mViewPage;
    LinearLayout mBottomBar;
    TextView mTitleView;
    private List<Fragment> mFragments = new ArrayList<>();
    private String[] toolBarTitleArr = {"会话","联系人"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_panel);
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                ToolBarUtil.changeColor(i);
                //修改title
                mTitleView.setText(toolBarTitleArr[i]);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        ToolBarUtil.setmOnToolbarClickListener(new ToolBarUtil.OnToolBarClickListener() {
            @Override
            public void onToolBarClick(int postion) {
                mViewPage.setCurrentItem(postion);
            }
        });
    }

    private void initView() {
        mViewPage = findViewById(R.id.viewPage);
        mBottomBar = findViewById(R.id.bottom_bar);
        mTitleView = findViewById(R.id.title_bar);
    }

    private void initData() {
        mFragments.add(SessionFragment.newInstance(null,null));
        mFragments.add(ContactsFragment.newInstance(null,null));
        mViewPage.setAdapter(new MyPagerAdapter(this.getSupportFragmentManager()));

        //图标内容
        int[] iconArr = {R.drawable.icon_message_selector,R.drawable.icon_user_selector};
        ToolBarUtil.createToolBar(mBottomBar,toolBarTitleArr,iconArr);

        //设置默认选中会话
        ToolBarUtil.changeColor(0);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get(i);
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
}
